home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / etch.zip / ETCH.C next >
C/C++ Source or Header  |  1994-02-07  |  7KB  |  305 lines

  1. /* This is a sample program that demonstrates how to make use of the mouse
  2.  * driver in SuperVGA modes.  The program also accepts an argument to
  3.  * indicate the desired 'mode'.  Valid modes may be found by looking in the
  4.  * graph.h header file.
  5.  * USE:  WCL386 (or WCL) -ox etch.c
  6.  * Note: -ox MUST be used or the stack pragmas will not work properly
  7.  */
  8.  
  9. #include <conio.h>
  10. #include <dos.h>
  11. #include <graph.h>
  12.  
  13.  
  14. void main( int argc, char *argv[] )
  15. //=================================
  16.  
  17. {
  18.     int                 mode;
  19.     struct videoconfig  vc;
  20.  
  21.     if( argc == 2 ) {
  22.     mode = atoi( argv[ 1 ] );
  23.     } else {
  24.     mode = _MAXRESMODE;
  25.     }
  26.     if( _setvideomode( mode ) == 0 ) {
  27.     printf( "No graphics adapter present\n" );
  28.     exit( 1 );
  29.     }
  30.     if( InitMouse() == 0 ) {
  31.     printf( "No mouse driver present\n" );
  32.     exit( 1 );
  33.     }
  34.     _getvideoconfig( &vc );
  35.     SetMouseRange( 0, 0, vc.numxpixels - 1, vc.numypixels - 1 );
  36.     _grtext( 5, 50, "Press <ESC> to clear screen, <END> to quit");
  37.     _grtext( 5, 80, "You may also enter a mode (ie. 258) as a");
  38.     _grtext( 5, 110, "parameter when invoking etch.exe");
  39.     Etch();
  40.     FiniMouse();
  41.     _setvideomode( _DEFAULTMODE );
  42. }
  43.  
  44.  
  45. void Etch( void )
  46. //===============
  47.  
  48. // Follow the mouse and draw while the mouse button is pressed.
  49. // If 'Esc' is pressed, clear the screen. If 'End' is pressed, exit.
  50.  
  51. {
  52.     int                 pen_down, button, ch;
  53.     struct xycoord      curr_pos, prev_pos;
  54.  
  55.     CursorOn();
  56.     pen_down = 0;
  57.  
  58.     for( ;; ) {
  59.     GetPosition( &curr_pos, &button );
  60.     if( button != 0 ) {     // button pressed
  61.         if( pen_down != 1 ) {
  62.         pen_down = 1;
  63.         _moveto( curr_pos.xcoord, curr_pos.ycoord );
  64.         prev_pos = curr_pos;
  65.         } else {
  66.         if( prev_pos.xcoord != curr_pos.xcoord || prev_pos.ycoord != curr_pos.ycoord ) {
  67.             CursorOff();
  68.             _lineto( curr_pos.xcoord, curr_pos.ycoord );
  69.             CursorOn();
  70.             prev_pos = curr_pos;
  71.         }
  72.         }
  73.     } else {
  74.         pen_down = 0;
  75.     }
  76.     if( kbhit() ) {
  77.         ch = getch();
  78.         if( ch == 0 ) {
  79.         ch = 256 + getch();
  80.         }
  81.         if( ch == 27 ) {              /* ESC key */
  82.         CursorOff();
  83.         _clearscreen( _GCLEARSCREEN );
  84.         CursorOn();
  85.         } else if( ch == 335 ) {      /* END key */
  86.         return;
  87.         }
  88.     }
  89.     }
  90. }
  91.  
  92.  
  93. // Mouse Library
  94.  
  95.  
  96. #define MOUSE_INT       0x33
  97.  
  98. int                     MouseCursorOn = 0;
  99. int                     MouseX;
  100. int                     MouseY;
  101. int                     WeDrawCursor = 0;
  102.  
  103. #if !defined( __386__ )
  104.   #pragma aux           GetStack = "mov dx,ss" \
  105.                    "mov ax,sp" value [ dx ax ];
  106.   #pragma aux           SetStack = "mov ss,dx" \
  107.                    "mov sp,ax" parm [ dx ax ];
  108.   #pragma aux           mouse_handler parm [ cx ] [ dx ]
  109. #else
  110.   #pragma aux           GetStack = "mov dx,ss" \
  111.                    "mov eax,esp" value [ dx eax ];
  112.   #pragma aux           SetStack = "mov ss,dx" \
  113.                    "mov esp,eax" parm [ dx eax ];
  114.   #pragma aux           mouse_handler parm [ ecx ] [ edx ]
  115. #endif
  116.  
  117. #define STACKSIZE       1024
  118.  
  119. extern void far         *GetStack();
  120. extern void             SetStack( char far * );
  121.  
  122.  
  123. static void _loadds far mouse_handler( int x, int y )
  124. //===================================================
  125.  
  126. {
  127.     static char         newstack[ STACKSIZE ];
  128.     static void far     *oldstack;
  129.  
  130.     if( MouseCursorOn ) {
  131.     oldstack = GetStack();
  132.     SetStack( newstack + STACKSIZE );
  133.     DrawCursor();
  134.     MouseX = x;
  135.     MouseY = y;
  136.     DrawCursor();
  137.     SetStack( oldstack );
  138.     } else {
  139.     MouseX = x;
  140.     MouseY = y;
  141.     }
  142. }
  143.  
  144.  
  145. static void DrawCursor()
  146. //======================
  147.  
  148. {
  149.     int                 old_act;
  150.     int                 old_style;
  151.     struct xycoord      old_pos;
  152.  
  153.     old_act = _setplotaction( _GXOR );
  154.     old_style = _getlinestyle();
  155.     _setlinestyle( 0xffff );
  156.     old_pos = _moveto( MouseX, MouseY );
  157.     _lineto( MouseX + 15, MouseY + 15 );
  158.     _moveto( MouseX, MouseY + 8 );
  159.     _lineto( MouseX, MouseY );
  160.     _lineto( MouseX + 8, MouseY );
  161.     _moveto( old_pos.xcoord, old_pos.ycoord );
  162.     _setlinestyle( old_style );
  163.     _setplotaction( old_act );
  164. }
  165.  
  166.  
  167. int InitMouse( void )
  168. //===================
  169.  
  170. {
  171.     int                 result;
  172.     union REGPACK       regs;
  173.     struct xycoord      pos;
  174.     struct videoconfig  vc;
  175.  
  176.     memset( ®s, 0, sizeof( union REGPACK ) );
  177.     regs.w.ax = 0;
  178.     intr( MOUSE_INT, ®s );
  179.     result = regs.w.ax;
  180.     if( result != 0 ) {
  181.     _getvideoconfig( &vc );
  182.     if( vc.mode >= _URES256COLOR ) {
  183.         // SuperVGA mode, we have to draw the mouse cursor ourselves
  184.         WeDrawCursor = 1;
  185.         regs.w.ax = 0x000C;
  186.         regs.w.cx = 0x0001;
  187. #if defined( __386__ )
  188.         regs.x.edx = FP_OFF( &mouse_handler );
  189. #else
  190.         regs.w.dx = FP_OFF( &mouse_handler );
  191. #endif
  192.         regs.w.es = FP_SEG( &mouse_handler );
  193.         intr( MOUSE_INT, ®s );
  194.         // place mouse in centre of screen
  195.         MouseX = vc.numxpixels / 2;
  196.         MouseY = vc.numypixels / 2;
  197.         pos.xcoord = MouseX;
  198.         pos.ycoord = MouseY;
  199.         SetPosition( &pos );
  200.         MouseCursorOn = 1;
  201.         DrawCursor();
  202.     }
  203.     }
  204.     return( result );
  205. }
  206.  
  207.  
  208. void CursorOn( void )
  209. //===================
  210.  
  211. {
  212.     union REGPACK       regs;
  213.  
  214.     if( WeDrawCursor ) {
  215.     if( !MouseCursorOn ) {
  216.         DrawCursor();
  217.     }
  218.     MouseCursorOn = 1;
  219.     } else {
  220.     memset( ®s, 0, sizeof( union REGPACK ) );
  221.     regs.w.ax = 1;
  222.     intr( MOUSE_INT, ®s );
  223.     }
  224. }
  225.  
  226.  
  227. void CursorOff( void )
  228. //====================
  229.  
  230. {
  231.     union REGPACK       regs;
  232.  
  233.     if( WeDrawCursor ) {
  234.     if( MouseCursorOn ) {
  235.         DrawCursor();
  236.     }
  237.     MouseCursorOn = 0;
  238.     } else {
  239.     memset( ®s, 0, sizeof( union REGPACK ) );
  240.     regs.w.ax = 2;
  241.     intr( MOUSE_INT, ®s );
  242.     }
  243. }
  244.  
  245.  
  246. void GetPosition( struct xycoord *pos, int *left )
  247. //================================================
  248.  
  249. {
  250.     union REGPACK       regs;
  251.  
  252.     memset( ®s, 0, sizeof( union REGPACK ) );
  253.     regs.w.ax = 3;
  254.     intr( MOUSE_INT, ®s );
  255.     pos->xcoord = regs.w.cx;
  256.     pos->ycoord = regs.w.dx;
  257.     *left = regs.w.bx & 1;
  258. }
  259.  
  260.  
  261. void SetPosition( struct xycoord *pos )
  262. //=====================================
  263.  
  264. {
  265.     union REGPACK       regs;
  266.  
  267.     memset( ®s, 0, sizeof( union REGPACK ) );
  268.     regs.w.ax = 4;
  269.     regs.w.cx = pos->xcoord;
  270.     regs.w.dx = pos->ycoord;
  271.     intr( MOUSE_INT, ®s );
  272. }
  273.  
  274.  
  275. void SetMouseRange( int x1, int y1, int x2, int y2 )
  276. //==================================================
  277.  
  278. {
  279.     union REGPACK       regs;
  280.  
  281.     memset( ®s, 0, sizeof( union REGPACK ) );
  282.     regs.w.ax = 7;
  283.     regs.w.cx = x1;
  284.     regs.w.dx = x2;
  285.     intr( MOUSE_INT, ®s );
  286.     regs.w.ax = 8;
  287.     regs.w.cx = y1;
  288.     regs.w.dx = y2;
  289.     intr( MOUSE_INT, ®s );
  290. }
  291.  
  292.  
  293. void FiniMouse()
  294. //==============
  295.  
  296. {
  297.     union REGPACK       regs;
  298.  
  299.     if( WeDrawCursor ) {    // init again, to clear handler
  300.     memset( ®s, 0, sizeof( union REGPACK ) );
  301.     regs.w.ax = 0;
  302.     intr( MOUSE_INT, ®s );
  303.     }
  304. }
  305.